﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.Xml;

public partial class Manager_Catalogue_AddCatalogue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
            BindKindNo();
            BindTopicList();
            if (Request.QueryString["CatID"] != "" && Request.QueryString["CatID"] != null)
            {
                trCatProp.Visible = false;
            }
            else
            {
                BindCatalogueByTopicID();
            }
            BindProperties(Request.QueryString["CatID"]);
        }
        else
        {
            if (hidXML.Value != "")
            {
                string strXML = "<PccMsg>" + hidXML.Value + "</PccMsg>";
                PccMsg msg = new PccMsg(strXML);
                string Method = msg.Query("Method");
                if (Method == "SaveCatalogue_Click")
                {
                    SaveCatalogue(msg);
                    hidXML.Value = "";
                }
            }
        }

        
        
    }


    private void BindKindNo()
    {

        ListItem item = new ListItem();
        item.Text = "Sản phẩm";
        item.Value = "P";
        item.Selected = true;
        ddlKindNo.Items.Add(item);
        item = new ListItem();
        item.Text = "Phim HD";
        item.Value = "F";
        ddlKindNo.Items.Add(item);

        try
        {
            ddlKindNo.SelectedItem.Selected = false;
            ddlKindNo.Items.FindByValue(Request.QueryString["KindNo"]).Selected = true;
        }
        catch { }

    }
    private void BindCatalogueByTopicID()
    {
        clsWebShop cls = new clsWebShop();

        ListItem item = new ListItem();
        DataTable dt = null;
        dt = cls.Get_CatalogueByTopicIDNoneProc(ddlChuDe.SelectedValue).Tables[0];
        ddlCatProp.DataSource = dt;
        ddlCatProp.DataTextField = "Catname";
        ddlCatProp.DataValueField = "CatID";
        ddlCatProp.DataBind();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlCatProp.Items.Insert(0, item);
        try
        {
            ddlCatProp.SelectedItem.Selected = false;
            if (Request.QueryString["CatID"] != "" && Request.QueryString["CatID"] != null)
                ddlCatProp.Items.FindByValue(Request.QueryString["CatID"]).Selected = true;
        }
        catch (Exception ex) { }
    }

    private void BindProperties(string CatID)
    {

        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        if (CatID != "" && CatID != null)
        {
            dt = cls.Get_cat_PropertiesByCatIDNoneProc(CatID).Tables[0];
            if (Request.QueryString["CatID"] != "" && Request.QueryString["CatID"] != null)
            {
                txtTheLoai.Text = Request.QueryString["CatName"].ToString();
                ddlChuDe.Items.FindByValue(Request.QueryString["TopicID"]).Selected = true;
            }

            DataRow row;
            row = dt.NewRow();
            dt.Rows.Add(row);


        }
        else
        {
            DataRow row;
            dt.Columns.Add("PrtID");
            dt.Columns.Add("PrtNm");
            row = dt.NewRow();
            dt.Rows.Add(row);
        }


        grv_Catalogue.DataSource = dt;
        grv_Catalogue.DataBind();
        try
        {
            grv_Catalogue.Rows[grv_Catalogue.Rows.Count - 1].Cells[1].Controls.Clear();
        }
        catch { }

    }


    private void BindTopicList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlChuDe.DataSource = cls.GetData("select DISTINCT * from Topic where Status<>'D' order by TopicName");
        ddlChuDe.DataTextField = "TopicName";
        ddlChuDe.DataValueField = "TopicID";
        ddlChuDe.DataBind();
        item.Text = "-- Chọn chủ đề --";
        item.Value = "0";
        ddlChuDe.Items.Insert(0, item);




    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ReturnPage();
    }

    private string QueryStringStr(string str)
    {
        return str + "=" + Request.QueryString[str];

    }
    private void ReturnPage()
    {
        Response.Redirect("Catalogue.aspx?CodeID=" + Request.QueryString["CodeID"] + "&" + QueryStringStr("sTopicID"));
    }

    private void SaveCatalogue(PccMsg msgPro)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        string CatID = "0";
        if (Request.QueryString["CatID"] != "" && Request.QueryString["CatID"] != null)
        {
            CatID = Request.QueryString["CatID"];
        }
        string strShowMK = "N";
        if (chk_state.Checked == true)
            strShowMK = "Y";

        msg.CreateFirstNode("CatID", CatID);
        msg.CreateFirstNode("KindNo", ddlKindNo.SelectedValue);
        msg.CreateFirstNode("TopicID", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("CatName", txtTheLoai.Text);
        msg.CreateFirstNode("ShowMK", strShowMK);
        msg.CreateFirstNode("strProperties", "");
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("UpdID", "0");




        string strXML = cls.Ins_Catalogue_Properties(msg.GetXmlStr.ToString());

        msg = new PccMsg(strXML);
        string strCatIDOut = msg.Query("strCatIDOut");
        if ( strCatIDOut!= "")
        {
            XmlNode node = msgPro.QueryNode("Properties");
            for (int i = 0; i < node.ChildNodes.Count / 2; i++)
            {
                string PrtNm = node.SelectNodes("PrtNm")[i].InnerXml;
                string PrtID = node.SelectNodes("PrtID")[i].InnerXml;
                if (PrtID == "") PrtID = "0";
                msg = new PccMsg();
                msg.CreateFirstNode("PrtID", PrtID);
                msg.CreateFirstNode("PrtNm", PrtNm);
                msg.CreateFirstNode("CatID", strCatIDOut);
                cls.Ins_cat_Properties(msg.GetXmlStr.ToString());
            }
        }


        ReturnPage();

        //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Success!!!');window.location.href='Catalogue.aspx?CodeID=" + Request.QueryString["CodeID"] + "</script>");

    }
    /*
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("TopicID", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("CatName", txtTheLoai.Text);
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("UpdID", "0");        
        string strXML=cls.Ins_Catalogue(msg.GetXmlStr.ToString());
        msg = new PccMsg(strXML);
        string strOutput=msg.Query("strOutput");
        string strCatIDOut=msg.Query("strCatIDOut");

        if (strOutput == "0" && strCatIDOut != "" && strCatIDOut != null)
        {
            if (grv_Catalogue.Rows.Count > 0)
            {
                for (int i = 0; i < grv_Catalogue.Rows.Count; i++)
                {
                    TextBox txtThuocTinh = (TextBox)grv_Catalogue.Rows[i].FindControl("txtThuocTinh");
                    if (txtThuocTinh.Text.Trim() != "")
                    {
                        msg = new PccMsg();
                        msg.CreateFirstNode("PrtNm", txtThuocTinh.Text);
                        msg.CreateFirstNode("CatID", strCatIDOut);
                        cls.Ins_cat_Properties(msg.GetXmlStr.ToString());
                    }

                }
            }
        }

        ReturnPage();

    }
    */
    protected void ddlCatProp_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProperties(ddlCatProp.SelectedValue);
    }
    protected void grv_Catalogue_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            clsWebShop cls = new clsWebShop();
            cls.UpdateData("delete from [cat_Properties] where [PrtID]=" + grv_Catalogue.DataKeys[e.RowIndex].Value.ToString() + "");
            BindProperties(Request.QueryString["CatID"]);
        }
        catch { }
    }
}
